home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Online / QMail / source / qmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-15  |  2.4 KB  |  104 lines

  1. #include "substdio.h"
  2. #include "readwrite.h"
  3. #include "wait.h"
  4. #include "exit.h"
  5. #include "fork.h"
  6. #include "fd.h"
  7. #include "qmail.h"
  8. #include "auto_qmail.h"
  9.  
  10. static char *binqqargs[2] = { "bin/qmail-queue", 0 } ;
  11.  
  12. int qmail_open(qq)
  13. struct qmail *qq;
  14. {
  15.   int pim[2];
  16.   int pie[2];
  17.  
  18.   if (pipe(pim) == -1) return -1;
  19.   if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; }
  20.  
  21.   switch(qq->pid = vfork()) {
  22.     case -1:
  23.       close(pim[0]); close(pim[1]);
  24.       close(pie[0]); close(pie[1]);
  25.       return -1;
  26.     case 0:
  27.       close(pim[1]);
  28.       close(pie[1]);
  29.       if (fd_move(0,pim[0]) == -1) _exit(120);
  30.       if (fd_move(1,pie[0]) == -1) _exit(120);
  31.       if (chdir(auto_qmail) == -1) _exit(120);
  32.       execv(*binqqargs,binqqargs);
  33.       _exit(120);
  34.   }
  35.  
  36.   qq->fdm = pim[1]; close(pim[0]);
  37.   qq->fde = pie[1]; close(pie[0]);
  38.   substdio_fdbuf(&qq->ss,write,qq->fdm,qq->buf,sizeof(qq->buf));
  39.   qq->flagerr = 0;
  40.   return 0;
  41. }
  42.  
  43. unsigned long qmail_qp(qq) struct qmail *qq;
  44. {
  45.   return qq->pid;
  46. }
  47.  
  48. void qmail_fail(qq) struct qmail *qq;
  49. {
  50.   qq->flagerr = 1;
  51. }
  52.  
  53. void qmail_put(qq,s,len) struct qmail *qq; char *s; int len;
  54. {
  55.   if (!qq->flagerr) if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
  56. }
  57.  
  58. void qmail_puts(qq,s) struct qmail *qq; char *s;
  59. {
  60.   if (!qq->flagerr) if (substdio_puts(&qq->ss,s) == -1) qq->flagerr = 1;
  61. }
  62.  
  63. void qmail_from(qq,s) struct qmail *qq; char *s;
  64. {
  65.   if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
  66.   close(qq->fdm);
  67.   substdio_fdbuf(&qq->ss,write,qq->fde,qq->buf,sizeof(qq->buf));
  68.   qmail_put(qq,"F",1);
  69.   qmail_puts(qq,s);
  70.   qmail_put(qq,"",1);
  71. }
  72.  
  73. void qmail_to(qq,s) struct qmail *qq; char *s;
  74. {
  75.   qmail_put(qq,"T",1);
  76.   qmail_puts(qq,s);
  77.   qmail_put(qq,"",1);
  78. }
  79.  
  80. int qmail_close(qq)
  81. struct qmail *qq;
  82. {
  83.   int wstat;
  84.  
  85.   qmail_put(qq,"",1);
  86.   if (!qq->flagerr) if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
  87.   close(qq->fde);
  88.  
  89.   if (wait_pid(&wstat,qq->pid) != qq->pid) return QMAIL_WAITPID;
  90.   if (wait_crashed(wstat)) return QMAIL_CRASHED;
  91.   switch(wait_exitcode(wstat)) {
  92.     case 0: if (qq->flagerr) return QMAIL_BUG; return 0;
  93.     case 112: return QMAIL_USAGE;
  94.     case 115: return QMAIL_TOOLONG;
  95.     case 103: case 104: case 105: case 106: case 108: return QMAIL_SYS;
  96.     case 121: return QMAIL_READ;
  97.     case 122: return QMAIL_WRITE;
  98.     case 123: return QMAIL_NOMEM;
  99.     case 124: return QMAIL_TIMEOUT;
  100.     case 120: return QMAIL_EXECSOFT;
  101.     default: /* 101 or 102 */ return QMAIL_BUG;
  102.   }
  103. }
  104.